package P1线性结构.LeetCode单调栈与单调队列;

import java.util.Arrays;
import java.util.LinkedList;

public class LC503 {
    public int[] nextGreaterElements(int[] nums) {
        int[] ret = new int[nums.length];
        Arrays.fill(ret, -1);
        LinkedList<Integer> stack = new LinkedList<>();
        for (int i = 0; i < nums.length * 2 - 1; i++) {
            while (!stack.isEmpty() && nums[i % nums.length] > nums[stack.peek()]) {
                ret[stack.pop()] = nums[i % nums.length];
            }
            stack.push(i % nums.length);
        }
        return ret;
    }
}